//index.js
//获取应用实例
const app = getApp()
var util = require('../../utils/util.js');
var getInfo = util.getInfo;
var coupon_image = util.coupon_image;
Page({
  /*data: {
    motto: '亲爱的小菲',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs' 
    })
  },*/
  data: {
    background: [
      { image: "../../image/slide2.png" },
      { image: "../../image/slide3.png" }
    ],
    indicatorDots: true,
    vertical: false,
    autoplay: true,
    interval: 2000,
    duration: 500,

    windowWidth: 0,

    // tabs: ['综合', '佣金', '销量', '价格'],
    tabs: [
      { name: '综合', order: '', status: 0 },
      { name: '佣金', order: 'yongjin', status: 0 },
      { name: '销量', order: 'sum', status: 0 },
      { name: '价格', order: 'price', status: 0 }
    ],
    nowTab: '',
    sort: '',
    stv: {
      windowWidth: 0,
      lineWidth: 0,
      offset: 0,
      tStart: false
    },
    activeTab: 0,

    category: [
      [
        { id: "16", title: "美妆", image: "../../image/beauty.png" },
        { id: "818", title: "服饰", image: "../../image/house.png" },
        { id: "1281", title: "鞋包", image: "../../image/shoes.png" },
        { id: "12", title: "海淘", image: "../../image/haitao.png" }
      ]
    ],

    goods: [],         // 商品列表
    page: 1,        // 当前页码
    fid: '',           // 商品种类
    isEnd: false,
    keyword: '',       // 搜索词
    sort: 'desc',

    goods_item_padding: 3,
    coupon_image: coupon_image,

    show_toast: false,
    show_toast2: false,

    _fid: 0,
    ad_flag: 0,    //是否显示广告页
    ad_info: null  //广告数据
  },
  onShow(){
    // wx.showTabBar();
    let that=this;
    let p={
      m:'index',
      uid: wx.getStorageSync('uid') ? wx.getStorageSync('uid'):'1',
      i:'2'
    };
    getInfo(p).then(res=>{
      that.setData({
        goods: res.result,
        page:'1',
        fid:'',
        keyword:'',
        _fid:0,
        nowTab:'',
        tabs: [
          { name: '综合', order: '', status: 0 },
          { name: '佣金', order: 'yongjin', status: 0 },
          { name: '销量', order: 'sum', status: 0 },
          { name: '价格', order: 'price', status: 0 }
        ],
        activeTab: 0,
        'stv.offset':0,
        sort: ''
      })
    })
  },
  onLoad: function () {
    
    var that = this;
   
    // let params = {
    //   m: 'check_we_app'
    // };
    // getInfo(params).then(res => {
    //   console.log(res);
    //   that.setData({
    //     ad_flag:res.result
    //   })
    // })



    try {
      let { tabs } = this.data;
      var res = wx.getSystemInfoSync()
      this.windowWidth = res.windowWidth;
      this.data.stv.lineWidth = this.windowWidth / this.data.tabs.length;
      this.data.stv.windowWidth = res.windowWidth;
      this.setData({ stv: this.data.stv })
      this.tabsCount = tabs.length;
    } catch (e) {
    }
    this.setData({
      windowWidth: wx.getSystemInfoSync().windowWidth,//图片宽度
    });

    if (!wx.getStorageSync('count')) {
      wx.setStorageSync('count', 1);
    }
    else {
      wx.setStorageSync('count', wx.getStorageSync('count') + 1);
    }

    // 请求页面加载默认展示的数据
    var defaultUrl = app.globalData.commonUrl;

    var p = {
      m: 'index',
      uid: wx.getStorageSync('uid') ? wx.getStorageSync('uid') : '1',
      i: '2'
    }
    getInfo(p).then(res => {
      console.log(res);
      that.setData({
        goods: res.result
      }, () => {

      });
      if (wx.getStorageSync('count') <= 5) {
        wx.showToast({
          title: '点击客服按钮，可领取两元现金红包',
          icon: 'none',
          duration: 3000,
          mask: true
        });
        var info = wx.getSystemInfoSync().system.toLowerCase();
        console.log(info);
        if (/^ios/.test(info)) {
          console.log('it is ios');
        }
        else if (/^android/.test(info)) {
          console.log('it is android');
          that.setData({
            show_toast: true
          })
        }
      }
    })
  },
  _updateSelectedPage(page) {
    let { tabs, stv, activeTab } = this.data;
    activeTab = page;
    this.setData({ activeTab: activeTab })
    stv.offset = stv.windowWidth * activeTab;
    this.setData({ stv: this.data.stv })
  },
  //tab选项卡
  handlerTabTap(e) {
    this._updateSelectedPage(e.currentTarget.dataset.index);
    //商品排序
    var index = e.currentTarget.dataset.index;
    var item = e.currentTarget.dataset.item;
    console.log(this.data.nowTab);
    var that = this;

    let p = {};
    that.data.page = 1;

    if (item.name == '价格') {
      if (item.name == that.data.nowTab.name) {
        if (item.status == 0) {
          item.status = 1;
          that.setData({
            tabs: [
              { name: '综合', order: '', status: 0 },
              { name: '佣金', order: 'yongjin', status: 0 },
              { name: '销量', order: 'sum', status: 0 },
              { name: '价格', order: 'price', status: 1 }
            ]
          })
          that.data.sort = 'desc';
          p = {
            m: 'index',
            fid: that.data.fid ? that.data.fid : '',
            search: that.data.keyword ? that.data.keyword : '',
            order: item.order,
            uid: wx.getStorageSync('uid'),
            sort: 'desc',
            i: '2'
          }
        }
        else if (item.status == 1) {
          item.status = 2;
          that.setData({
            tabs: [
              { name: '综合', order: '', status: 0 },
              { name: '佣金', order: 'yongjin', status: 0 },
              { name: '销量', order: 'sum', status: 0 },
              { name: '价格', order: 'price', status: 2 }
            ]
          })
          that.data.sort = 'asc';
          p = {
            m: 'index',
            fid: that.data.fid ? that.data.fid : '',
            search: that.data.keyword ? that.data.keyword : '',
            order: item.order,
            uid: wx.getStorageSync('uid'),
            sort: 'asc',
            i: '2'
          }
        }
        else if (item.status == 2) {
          item.status = 1;
          that.data.sort = 'desc';
          that.setData({
            tabs: [
              { name: '综合', order: '', status: 0 },
              { name: '佣金', order: 'yongjin', status: 0 },
              { name: '销量', order: 'sum', status: 0 },
              { name: '价格', order: 'price', status: 1 }
            ]
          })
          p = {
            m: 'index',
            fid: that.data.fid ? that.data.fid : '',
            search: that.data.keyword ? that.data.keyword : '',
            order: item.order,
            uid: wx.getStorageSync('uid'),
            sort: 'desc',
            i: '2'
          }
        }
      }
      else {
        that.setData({
          tabs: [
            { name: '综合', order: '', status: 0 },
            { name: '佣金', order: 'yongjin', status: 0 },
            { name: '销量', order: 'sum', status: 0 },
            { name: '价格', order: 'price', status: 0 }
          ]
        });
        p = {
          m: 'index',
          fid: that.data.fid ? that.data.fid : '',
          search: that.data.keyword ? that.data.keyword : '',
          order: item.order,
          uid: wx.getStorageSync('uid'),
          i: '2'
        }
      }
    }
    else {
      that.setData({
        tabs: [
          { name: '综合', order: '', status: 0 },
          { name: '佣金', order: 'yongjin', status: 0 },
          { name: '销量', order: 'sum', status: 0 },
          { name: '价格', order: 'price', status: 0 }
        ]
      });
      that.data.sort = 'desc';
      p = {
        m: 'index',
        order: item.order,
        fid: that.data.fid ? that.data.fid : '',
        sort: 'desc',
        uid: wx.getStorageSync('uid'),
        search: that.data.keyword ? that.data.keyword : '',
        i: '2'
      }
    }
    getInfo(p).then(res => {
      that.setData({
        goods: res.result
      })
    })


    that.setData({
      tabs: that.data.tabs
    });
    that.data.nowTab = item;
    that.setData({
      nowTab: item
    })

  },

  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },

  /**
    * item点击事件
    */
  onIpItemClick: function (event) {
    console.log(event);
    var id = event.currentTarget.dataset.item.id;
    var curIndex = 0;
    for (var i = 0; i < this.data.ips.length; i++) {
      if (id == this.data.ips[i].id) {
        this.data.ips[i].isSelect = true;
        curIndex = i;
      } else {
        this.data.ips[i].isSelect = false;
      }
    }

    this.setData({
      ips: this.data.ips,
    });
  },

  // 触底加载更多
  onReachBottom() {
    console.log(this.data.nowPage);
    var defaultUrl = app.globalData.commonUrl;
    var that = this;
    that.data.page++;
    let p = {
      m: 'index',
      fid: that.data.fid ? that.data.fid : '',
      search: that.data.keyword ? that.data.keyword : '',
      order: that.data.nowTab.order ? that.data.nowTab.order : '',
      uid: wx.getStorageSync('uid'),
      sort: that.data.sort,
      page: that.data.page,
      i: '2'
    };
    getInfo(p).then(res => {
      that.setData({
        goods: [...that.data.goods, ...res.result]
      })
    })
  },
  // 选择分类
  chooseCategory(e) {
    this.setData({
      tabs: [
        { name: '综合', order: '', status: 0 },
        { name: '佣金', order: 'yongjin', status: 0 },
        { name: '销量', order: 'sum', status: 0 },
        { name: '价格', order: 'price', status: 0 }
      ]
    });
    this._updateSelectedPage(0);
    var fid = e.currentTarget.dataset.id;
    this.data.fid = fid;
    this.data.page = 1;
    this.data.keyword = '';
    var that = this;
    console.log(fid);
    let p={};
    if(fid=='818'){
      this.data.fid = '';
      this.data.keyword = '服饰';
      p={
        m: 'index',
        search:'服饰',
        uid: '10',
        i: '2'
      }
    }
    else{
      this.data.keyword = '';
      p = {
        m: 'index',
        fid: fid,
        uid: '10',
        i: '2'
      }
    }
    
    getInfo(p).then(res => {
      console.log(res);
      that.setData({
        goods: res.result,
        _fid: fid
      })
    })
  },
  copy_words(e) {
    // 保存输入的关键词
    this.data.keyword = e.detail.value;
  },
  // 点击回车搜索商品
  searchGoods(e) {
    var keyword = e.detail.value;
    this.data.keyword = keyword;
    var that = this;
    this.search();
  },
  // 点击图标按钮搜索商品
  search_btn() {
    this.search();
  },
  // 搜索
  search() {
    var that = this;
    this.data.fid = '';
    this.data.page = 1;
    let p = {
      m: 'index',
      search: that.data.keyword,
      uid: wx.getStorageSync('uid'),
      i: '2'
    };
    getInfo(p).then(res => {
      that.setData({
        goods: res.result
      })
    })
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮
      console.log(res.target)
    }
    return {
      title: '惠赚街',
      path: 'pages/index/index',
      success: function (res) {
        // 转发成功
      },
      fail: function (res) {
        // 转发失败
      }
    }
  },
  close_toast() {
    let that = this;
    that.setData({
      show_toast: false
    })
  },
  close_toast2() {
    let that = this;
    that.setData({
      show_toast2: false
    })
  },
  openShare() {
    // wx.hideTabBar();
    let that=this;
    that.setData({
      show_toast2: true
    })
  },
  submitFormId(e) {
    console.log(e);
    let formId = e.detail.formId;
    let that = this;
    let p = {
      m: 'colform',
      uid: wx.getStorageSync('uid'),
      formId: formId
    };
    getInfo(p).then(res => {})
  }
})
